package com.tbit.uqbike.push.service.impl;

import com.tbit.uqbike.push.constant.ControlTypeConstant;
import com.tbit.uqbike.push.constant.SystemData;
import com.tbit.uqbike.push.dao.core.FleetMachineBoundDao;
import com.tbit.uqbike.push.dao.core.ForbiddenDao;
import com.tbit.uqbike.push.pojo.*;
import com.tbit.uqbike.push.service.*;
import com.tbit.uqbike.push.util.DateTimeUtil;
import com.tbit.uqbike.push.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service("quartzJobService")
public class QuartzJobServiceImpl implements QuartzJobService {
    @Autowired
    private GeoService geoService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private TerControlService terControlService;
    @Autowired
    private MachineBorrowService machineBorrowService;
    @Autowired
    private ForbiddenDao forbiddenDao;
    @Autowired
    private FleetMachineBoundDao fleetMachineBoundDao;
    @Autowired
    private MachineAlarmService machineAlarmService;

    @Override
    @Scheduled(cron = "0 0/1 * * * ?")
    public void synDeoData() {
        //初始化
        LogUtil.info("电子围初始化");
        Map<Integer, List<Geo>> geoMap = new ConcurrentHashMap<>();
        List<Geo> geos = geoService.getAll();

        /**组装数据*/
        if (geos.size() > 0) {
            for (Geo geo : geos) {
                if (!geoMap.containsKey(geo.getAccountId())) {
                    geoMap.put(geo.getAccountId(), new ArrayList<Geo>());
                }
                geoMap.get(geo.getAccountId()).add(geo);
            }
            LogUtil.info("同步电子围栏数量[" + geos.size() + "]");
        }
        SystemData.accountIdGeoListMap = geoMap;

        Map<Integer, List<Forbidden>> forbiddenMap = new ConcurrentHashMap<>();
        List<Forbidden> forbiddens = forbiddenDao.getAll();
        if (!forbiddens.isEmpty()) {
            for (Forbidden forbidden : forbiddens) {
                if (forbiddenMap.containsKey(forbidden.getAccountId())) {
                    forbiddenMap.get(forbidden.getAccountId()).add(forbidden);
                } else {
                    List<Forbidden> list = new ArrayList<>();
                    list.add(forbidden);
                    forbiddenMap.put(forbidden.getAccountId(), list);
                }
            }
            SystemData.FleetIdToForbiddenMap = forbiddenMap;
        }


        Map<Integer, Integer> machineAlarmGeoOut = new ConcurrentHashMap<>();
        List<MachineAlarm> machineAlarms = machineAlarmService.getAll();
        if (machineAlarms != null && !machineAlarms.isEmpty()) {
            for (MachineAlarm machineAlarm : machineAlarms) {
                if (machineAlarm.getGeoOutBoundsSwitch() != 0 && machineAlarm.getGeoOutSpeed() != 0) {
                    machineAlarmGeoOut.put(machineAlarm.getAccountId(), machineAlarm.getGeoOutSpeed());
                }
            }
        }
        if (machineAlarmGeoOut != null && !machineAlarmGeoOut.isEmpty()) {
            SystemData.machineAlarm = machineAlarmGeoOut;
        }

    }

    @Override
    @Scheduled(cron = "0 0/10 * * * ?")
    public void synNewMachineData() {
        String nowTime = DateTimeUtil.getNowTime();
        List<MachineBase> machineBases = null;
        /**加载新添加设备数据*/

        LogUtil.info("设备加载");
        machineBases = machineService.getAllMachineBase();

        if (machineBases.size() > 0) {
            for (MachineBase machineBase : machineBases) {
                SystemData.machineNOMachineIdMap.put(machineBase.getMachineNO(), machineBase.getMachineId());
                SystemData.machineIdAccountIdMap.put(machineBase.getMachineId(), machineBase.getAccountId());
                SystemData.machineNOMachineTypeMap.put(machineBase.getMachineNO(), machineBase.getMachineType());
            }
        }
        LogUtil.info("同步新设备数据[" + machineBases.size() + "]");
        /**更新同步时间*/
        SystemData.lastSynTime = nowTime;

        List<FleetMachineBound> bounds = fleetMachineBoundDao.getAll();
        if (!bounds.isEmpty()) {
            for (FleetMachineBound fleetMachineBound : bounds) {
                SystemData.machineIdFleetIdMap.put(fleetMachineBound.getMachineId(), fleetMachineBound.getFleetId());
            }
        }
    }

    @Override
    @Scheduled(cron = "0/30 * * * * ?")
    public void voiceNotice() {
        Map<String, String> outmachine = SystemData.outMachineNo;
        LogUtil.info("语音通知发送[" + outmachine.size() + "]");
        for (String machineNO : outmachine.keySet()) {
            MachineBorrow machineBorrow = machineBorrowService.getByMachineId(machineService.getByMachineNO(machineNO).getMachineId());
            if (machineBorrow != null) {
                LogUtil.info("设备在电子围栏外面，语音播报" + machineNO);
                String sNo = UUID.randomUUID().toString();
                terControlService.sendControl(sNo, machineNO, ControlTypeConstant.CONTROL_TYPE_VOICE, ControlTypeConstant.TER_VOICE_3, "", "");
            }
        }
    }

    @Override
    @Scheduled(cron = "0/60 * * * * ?")
    public void synBorrowingMachine() {
        /**查询当前借出设备*/
        List<MachineBorrow> machineBorrows = machineBorrowService.getAll();
        LogUtil.info("同步当前借车车辆数据[" + machineBorrows.size() + "]");
        if (machineBorrows.size() > 0) {
            Map<Integer, MachineBorrow> machineBorrowMap = new ConcurrentHashMap<Integer, MachineBorrow>();
            for (MachineBorrow mb : machineBorrows) {
                machineBorrowMap.put(mb.getMachineId(), mb);
            }

            /**更新内存数据*/
            SystemData.machineBorrowMap = machineBorrowMap;
        }
    }
}
